package cool.zhoujie.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import cool.zhoujie.MainActivity;
import cool.zhoujie.R;
import cool.zhoujie.db.DBUtil;
import cool.zhoujie.db.RecentPlay;
import cool.zhoujie.service.PlayService;

/**
 * 歌曲播放碎片
 */
public class PlayFragment extends BaseLazyLoadFragment {

    private ImageView songPic;

    private ImageView play_btn;

    private TextView songName;

    private TextView artistName;

    private PlayService playService;

    int current_state;

    public static final int STATE_NORMAL = 0;

    public static final int STATE_PLAY = 1;

    private RecentPlay recentPlay;

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_play, container, false);
        songPic = view.findViewById(R.id.play_songPic);
        play_btn = view.findViewById(R.id.btn_play);
        songName = view.findViewById(R.id.play_songName);
        artistName = view.findViewById(R.id.play_artist);
        return view;
    }

    @Override
    public void initEvent() {
        play_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity mainActivity = (MainActivity) getActivity();
                playService = mainActivity.playBinder.getService();

                playService.initMediaPlayer(recentPlay.songPath);
                startPlay();
            }
        });
    }

    @Override
    public void onLazyLoad() {
        List<RecentPlay> recentPlayList = DBUtil.queryAll();
        if (recentPlayList.size() > 0) {
            recentPlay = recentPlayList.get(recentPlayList.size() - 1);
            songName.setText(recentPlay.songName);
            artistName.setText(recentPlay.artistName);
            Glide.with(getActivity())
                    .load(recentPlay.songPic)
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                    .into(songPic);
        }
    }

    void updateView(final RecentPlay mRecentPlay) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Glide.with(getActivity())
                        .load(mRecentPlay.songPic)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                        .into(songPic);
                songName.setText(mRecentPlay.songName);
                artistName.setText(mRecentPlay.artistName);
                play_btn.setImageResource(R.drawable.btn_pause);
            }
        });
    }

    private void startPlay() {
        switch (current_state) {
            case STATE_NORMAL:
                playMusic();
                break;
            case STATE_PLAY:
                stopMusic();
                break;
            default:
                break;
        }
    }

    private void stopMusic() {
        play_btn.setImageResource(R.drawable.btn_play);
        current_state = STATE_NORMAL;
        playService.pause();
    }

    private void playMusic(){
        play_btn.setImageResource(R.drawable.btn_pause);
        current_state = STATE_PLAY;
        playService.play();
    }

}
